'use strict';

exports.main = async (event, context) => {
  const db = uniCloud.database();
  const {
    type = 'post', // post, activity
    page = 1,
    pageSize = 20,
    status = '',
    category = '',
    keyword = ''
  } = event;

  try {
    // 构建查询条件
    let where = { content_type: type };
    
    if (status && status !== 'all') {
      where.status = status;
    }
    
    if (category && category !== 'all') {
      where.category = category;
    }
    
    if (keyword) {
      where = {
        ...where,
        $or: [
          { title: new RegExp(keyword, 'i') },
          { content: new RegExp(keyword, 'i') }
        ]
      };
    }

    const collection = db.collection('add-content');
    
    // 获取总数
    const countResult = await collection.where(where).count();
    const total = countResult.total;

    // 获取数据并关联用户表
    const result = await collection
      .aggregate()
      .match(where)
      .lookup({
        from: 'uni-id-users',
        localField: 'user_id',
        foreignField: '_id',
        as: 'userInfo'
      })
      .sort({ create_time: -1 })
      .skip((page - 1) * pageSize)
      .limit(pageSize)
      .end();

    // 数据格式化
    const data = result.data.map(item => {
      // 获取关联的真实用户信息
      const realUser = item.userInfo && item.userInfo.length > 0 ? item.userInfo[0] : null;
      
      return {
        _id: item._id,
        content_type: item.content_type,
        title: item.title || '',
        category: item.category,
        content: item.content,
        files: item.files || [],
        user_id: item.user_id,
        // 发布者信息 - 优先使用真实用户信息
        publisher_name: realUser?.nickname || item.publisher_name || item.user_name || '未知用户',
        user_name: realUser?.nickname || item.user_name || item.publisher_name || '未知用户',
        publisher: {
          nickname: realUser?.nickname || item.publisher_name || item.user_name || '未知用户',
          avatar: realUser?.avatar || item.publisher_avatar || ''
        },
        user_info: {
          nickname: realUser?.nickname || '未知用户',
          username: realUser?.username || item.user_id || '',
          avatar: realUser?.avatar || item.publisher_avatar || ''
        },
        create_time: item.create_time,
        status: item.status,
        like_count: item.like_count || 0,
        comment_count: item.comment_count || 0,
        // 活动特有字段
        activity_time: item.activity_time,
        location: item.location,
        max_attendees: item.max_attendees || 0,
        attendee_count: item.attendee_count || 0,
        // 审核相关
        audit_status: item.audit_status || 'pending',
        audit_time: item.audit_time,
        audit_admin: item.audit_admin,
        reject_reason: item.reject_reason,
        // 置顶标记
        pinned: item.pinned || false,
        pinned_time: item.pinned_time,
        // 违规计数
        violation_count: item.violation_count || 0
      };
    });

    return {
      code: 200,
      msg: 'success',
      data: data,
      total: total,
      page: page,
      pageSize: pageSize
    };
  } catch (error) {
    console.error('获取社区内容失败:', error);
    return {
      code: 500,
      msg: '获取内容失败',
      data: [],
      total: 0
    };
  }
};
